/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tbenglish_dal;

import bllInterface.ChatMessage;
import bllInterface.ChatMessageContent;
import dalInterface.DALFactory;
import dalfactory.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import sqlconnect.*;
import utils.DataTable;
import utils.DefaultPort;

/**
 *
 * @author salm
 */
public class TBEnglish_DAL {

    private static String DALHost;
    
    private static void readHost()
    {
        // NEED TABLE
        try
        {
            Properties p = new Properties();
            p.load(new FileInputStream("src\\RMIConfig.ini"));
            
            DALHost = p.getProperty("DALHost");
        }
        catch (IOException ioEx)
        {
            DALHost = "rmi://localhost";
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws RemoteException {
        try
        {
            readHost();
            // TODO code application logic here
            Connection conn = SQLConnectionFactory.getInstance().createConnectionByConfigFile("src\\ServerConfig.ini").getSqlConnection();
            System.out.println("DB connected");
            
            LocateRegistry.createRegistry(DefaultPort.getInstance().getDALPort());
            DALFactory dalFact = new DALFactoryImpl(conn);
            DALFactory exporetdDALFact = (DALFactory) UnicastRemoteObject.exportObject(dalFact, DefaultPort.getInstance().getDALPort());
            Naming.rebind(String.format("%s:%d/%s", DALHost, DefaultPort.getInstance().getDALPort(), "DALFactory"), exporetdDALFact);
            //hoặc r.bind(“server”, server);
            
            DataTable chatMsgs = dalFact.getMessage().getLimitedMessagesFrom("Less", 10);
            System.out.println(chatMsgs);
        }
        catch (SQLException ex)
        {
            System.out.println("Can't connect to database");
            Logger.getLogger(TBEnglish_DAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (MalformedURLException ex) {
            Logger.getLogger(TBEnglish_DAL.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
